package org.redmagic.ui;

import java.util.Set;

import org.redmagic.common.MagicProperty;
import org.redmagic.domain.InternalServiceProperty;

import com.fasterxml.jackson.annotation.JsonProperty;

public class UiProperty extends MagicProperty {
	
	public static UiProperty fromUiProperty(InternalServiceProperty property) {
		return new UiProperty(property.getKey(), property.getName(), property.getTags(), 
				property.getValue(), property.getGroup(), property.getEncoding());
	}
	
	private final String key;
	private final Set<String> tags;
	private final String group;

	public UiProperty(String key, String name, Set<String> tags, String value, String group) {
		super(name, value);
		this.key = key;
		this.tags = tags;
		this.group = group;
	}

	public UiProperty(String key, String name, Set<String> tags, String value, String group, String encoding) {
		super(name, value, encoding);
		this.key = key;
		this.tags = tags;
		this.group = group;
	}
 
	@JsonProperty("key")
	public String getKey() {
		return key;
	}
 
    public Set<String> getTags() {
    	return tags;
    }
    
    @JsonProperty("tags")
    public String getPrettyTags() {
        return prettifyTags(tags);
    }
    
    private static String prettifyTags(Set<String> tags) {
		StringBuilder sb = new StringBuilder();
		for (String tag : tags) {
			sb.append("<span class=\"tag\">");
			sb.append(tag);
			sb.append("</span>");
		}
		
		
		return sb.toString();
	}
    
    @JsonProperty("group")
    public String getGroup() {
    	return group;
    }
    
    @Override
    public String toString() {
    	return "Key: " + key + ", Name: " + this.getName() + ", Tags: " + tags + ", Value: " + this.getValue() + ", Group: " + group;
    }
}